package com.template.templateuser.domain.mapper;

import com.template.templateuser.domain.dto.SysDeptDTO;
import com.template.templateuser.domain.web.param.FindSysDeptListParam;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface SysDeptMapper {



    @Select({
            "<script>",
            "select count(*) from sys_dept where deleted=0",
            "<if test=\"findSysDeptListParam.deptName != null  and findSysDeptListParam.deptName !='' \" > ",
            " and dept_name LIKE CONCAT('%', #{findSysDeptListParam.deptName}, '%') ",
            "  </if> ",
            "order by order_num",
            "</script>",
    })
    int getSysDeptCount(@Param("findSysDeptListParam") FindSysDeptListParam findSysDeptListParam );


    @Select({
            "<script>",
            "SELECT a.*,b.dept_name AS parent_name FROM `sys_dept` a LEFT JOIN `sys_dept`  b  ON a.parent_id=b.id where a.deleted=0",
            "<if test=\"findSysDeptListParam.deptName != null  and findSysDeptListParam.deptName !='' \" > ",
            " and a.dept_name LIKE CONCAT('%', #{findSysDeptListParam.deptName}, '%') ",
            "  </if> ",
            "order by a.order_num",
            "LIMIT #{offset},#{pageSize}",
            "</script>",
    })
    List<SysDeptDTO> getSysDeptList(@Param("findSysDeptListParam") FindSysDeptListParam findSysDeptListParam,@Param("offset") int offset, @Param("pageSize") int pageSize );


    @Select({
            "<script>",
            "SELECT * from sys_dept  where deleted=0",
            "</script>",
    })
    List<SysDeptDTO> getSysDeptAllList( );



    @Select({
            "<script>",
            "SELECT * from sys_dept  where parentId=#{parentId}  and deleted=0",
            "</script>",
    })
    List<SysDeptDTO> getSysDeptListByParentId(@Param("parentId") Long parentId );


    /**
     * 通过id获取部门信息
     *
     * @param id
     * @return
     */
    @Select({
            "<script>",
            "select * from sys_dept where deleted=0 and id=#{id}",
            "</script>",
    })
    SysDeptDTO findSysDeptById(@Param("id") Long id);


    /**
     * 保存
     * @param sysDeptDTO
     * @return
     */
    @Insert({
            "<script>",
            "INSERT INTO sys_dept",
            "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\" >",
            "   <if test=\"parentId != null\" >",
            "       parent_id,",
            "   </if>",
            "   <if test=\"deptName != null\" >",
            "       dept_name,",
            "   </if>",
            "   <if test=\"orderNum != null\" >",
            "       order_num,",
            "   </if>",
            "   <if test=\"leaderId != null\" >",
            "       leader_id,",
            "   </if>",
            "   <if test=\"phone != null\" >",
            "       phone,",
            "   </if>",
            "   <if test=\"email != null\" >",
            "       email,",
            "   </if>",
            "   <if test=\"status != null\" >",
            "       status,",
            "   </if>",
            "   <if test=\"deleted != null\" >",
            "       deleted,",
            "   </if>",
            "   <if test=\"createId != null\" >",
            "       create_id,",
            "   </if>",
            "   <if test=\"createTime != null\" >",
            "       create_time,",
            "   </if>",
            "   <if test=\"updateTime != null\" >",
            "       update_time,",
            "   </if>",
            "</trim>",
            "<trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\" >",
            "   <if test=\"parentId != null\" >",
            "       #{parentId},",
            "   </if>",
            "   <if test=\"deptName != null\" >",
            "       #{deptName},",
            "   </if>",
            "   <if test=\"orderNum != null\" >",
            "       #{orderNum},",
            "   </if>",
            "   <if test=\"leaderId != null\" >",
            "       #{leaderId},",
            "   </if>",
            "   <if test=\"phone != null\" >",
            "       #{phone},",
            "   </if>",
            "   <if test=\"email != null\" >",
            "       #{email},",
            "   </if>",
            "   <if test=\"status != null\" >",
            "       #{status},",
            "   </if>",
            "   <if test=\"deleted != null\" >",
            "       #{deleted},",
            "   </if>",
            "   <if test=\"createId != null\" >",
            "       #{createId},",
            "   </if>",
            "   <if test=\"createTime != null\" >",
            "       #{createTime},",
            "   </if>",
            "   <if test=\"updateTime != null\" >",
            "       #{updateTime},",
            "   </if>",
            "</trim>",
            "</script>",
    })
    @SelectKey(resultType = long.class, keyColumn = "id", before = false, statement = "SELECT LAST_INSERT_ID() AS id",
            keyProperty = "id")
    int save(SysDeptDTO sysDeptDTO);

    /**
     * 更新
     *
     * @param sysDeptDTO
     * @return java.lang.Integer
     *
     */
    @Update({
            "<script>",
            "update `sys_dept` ",
            "<set > ",
            "  <if test=\"parentId != null\" > ",
            "    parent_id = #{parentId}, ",
            "  </if> ",
            "  <if test=\"deptName != null\" > ",
            "    dept_name = #{deptName}, ",
            "  </if> ",
            "  <if test=\"orderNum != null\" > ",
            "    order_num = #{orderNum}, ",
            "  </if> ",
            "  <if test=\"leaderId != null\" > ",
            "    leader_id = #{leaderId}, ",
            "  </if> ",
            "  <if test=\"phone != null\" > ",
            "    phone = #{phone}, ",
            "  </if> ",
            "  <if test=\"email != null\" > ",
            "    email = #{email}, ",
            "  </if> ",
            "  <if test=\"status != null\" > ",
            "    status = #{status}, ",
            "  </if> ",
            "  <if test=\"deleted != null\" > ",
            "    deleted = #{deleted}, ",
            "  </if> ",
            "  <if test=\"updateTime != null\" > ",
            "    update_time = #{updateTime}, ",
            "  </if> ",
            "</set> ",
            "   where id = #{id}",
            "</script>",
    })
    Integer updateById(SysDeptDTO sysDeptDTO);





}
